package com.qf.config;

import com.qf.shiro.MyRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager")DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        Map<String, String> map = new HashMap<>();

        map.put("/login", "anon");
        map.put("/register","anon");
        map.put("/findAll","roles[manager]");
        map.put("/findByUid/{uid}","roles[manager]");
        map.put("/findBookByBid/{bid}","roles[manager]");

        map.put("/findAllBookclass","roles[manager]");
        map.put("/addBooks","roles[manager,author]");
        map.put("/updateBooks","roles[manager,author]");
        map.put("/deleteBooks/{bid}","roles[manager,author]");
        map.put("/addRack","anon");

        map.put("/deleteRack/{uid}","anon");
        map.put("/findRackByUid/{uid}","anon");
        map.put("/findPermissionBypid/{pid}","roles[manager]");
        map.put("/findAllPermission","roles[manager]");
        map.put("/addPermission","roles[manager]");

        map.put("/updatePermission","roles[manager]");
        map.put("/deletePermission/{pid}","roles[manager]");
        map.put("/findRoleByrid/{rid}","roles[manager]");
        map.put("/findAllRole","roles[manager]");
        map.put("/addRole","roles[manager]");

        map.put("/updateRole","roles[manager]");
        map.put("/deleteRole/{rid}","roles[manager]");
        map.put("/addBookclass","roles[manager]");
        map.put("/updateBookclass","roles[manager]");
        map.put("/deleteBookclass/{bcid}","roles[manager]");

        map.put("/findAllRolePerm","roles[manager]");
        map.put("/addRolePerm","roles[manager]");
        map.put("/updateRolePerm","roles[manager]");
        map.put("/deleteRolePerm/{rpid}","roles[manager]");
        map.put("/findAllUserdetails","roles[manager]");

        map.put("/addUserdetails","roles[manager]");
        map.put("/updateUserdetails","roles[manager]");
        map.put("/deleteUserdetails/{udid}","roles[manager]");
        map.put("/findAllUserInfo","roles[manager]");
        map.put("/addUserInfo","roles[manager]");

        map.put("/updateUserInfo","roles[manager]");
        map.put("/deleteUserInfo/{uuid}","roles[manager]");
        map.put("/findAllUserRole","roles[manager]");
        map.put("/addUserRole","roles[manager]");
        map.put("/updateUserRole","roles[manager]");

        map.put("/deleteUserRole/{urid}","roles[manager]");
        map.put("/findAllBBClass","roles[manager]");
        map.put("/addBBClass","roles[manager]");
        map.put("/updateBBClass","roles[manager]");
        map.put("/deleteBBClass/{bbid}","roles[manager]");

        map.put("/findBBClassByBbid/{bbid}","roles[manager]");
        map.put("/findBookclassByBcid/{bcid}","roles[manager]");
        map.put("/findByBid/{bid}","roles[manager]");
        map.put("/findUserdetailsByUdid/{udid}","roles[manager]");
        map.put("/findRolePermByid/{rpid}","roles[manager]");

        map.put("/findByUuid/{uuid}","roles[manager]");
        map.put("/findByUrid/{urid}","roles[manager]");
        map.put("/findBook","roles[manager]");


        shiroFilterFactoryBean.setLoginUrl("/login");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        return shiroFilterFactoryBean;
    }

    /**
     * 注解WebSecurityManager对象
     * @param myrealm
     * @return
     */
    @Bean(name = "defaultWebSecurityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("myRealm") MyRealm myrealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();

        defaultWebSecurityManager.setRealm(myrealm);

        return defaultWebSecurityManager;
    }

    /**
     * 开启Shiro的注解
     *  需要借助SpringAOP扫描shiro注解的类，来进行安全校验
     * @return
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();

        advisorAutoProxyCreator.setProxyTargetClass(true);

        return advisorAutoProxyCreator;
    }

    /**
     * 开启AOP的注解支持
     * @param defaultWebSecurityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager defaultWebSecurityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();

        authorizationAttributeSourceAdvisor.setSecurityManager(defaultWebSecurityManager);

        return authorizationAttributeSourceAdvisor;
    }
}
